package cn.tedu.qxhouserent.house.controller;

import cn.tedu.qxhouserent.commons.pojo.vo.PageData;
import cn.tedu.qxhouserent.commons.security.LoginPrincipal;
import cn.tedu.qxhouserent.house.pojo.param.UserAddNewParam;
import cn.tedu.qxhouserent.house.pojo.param.UserHouseCollectionAddNewParam;
import cn.tedu.qxhouserent.house.pojo.param.UserNewsCollectionAddNewParam;
import cn.tedu.qxhouserent.house.pojo.param.UserLoginInfoParam;
import cn.tedu.qxhouserent.house.pojo.param.UserUpdateInfoParam;
import cn.tedu.qxhouserent.house.pojo.vo.*;
import cn.tedu.qxhouserent.house.service.UserHouseCollectionService;
import cn.tedu.qxhouserent.house.service.UserNewsCollectionService;
import cn.tedu.qxhouserent.house.service.UserService;
import cn.tedu.qxhouserent.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户管理模块")
@Validated
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 50)
    public JsonResult login(UserLoginInfoParam userLoginInfoParam,
                            @ApiIgnore HttpServletRequest request) {
        log.debug("开始处理用户登录业务,参数 {}",userLoginInfoParam);
//        String remoteAddr = request.getRemoteAddr();
        // 处理登录
        String jwt = userService.login(userLoginInfoParam);
        return JsonResult.ok(jwt);
    }

    @Autowired
    private UserHouseCollectionService userHouseCollectionService;

    @Autowired
    private UserNewsCollectionService userNewsCollectionService;

    @PostMapping("add-new")
    @ApiOperation("添加用户")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid UserAddNewParam userAddNewParam){
        log.debug("开始处理添加用户业务");
        userService.addNew(userAddNewParam);

        return JsonResult.ok();
    }

    @PostMapping("add-new-house-collection")
    @ApiOperation("添加用户收藏房源")
    @ApiOperationSupport(order = 110)
    public JsonResult addNewHouseCollection(@AuthenticationPrincipal @ApiIgnore LoginPrincipal loginPrincipal,Long houseId) {
        log.debug("开始处理添加用户收藏业务,参数:当事人： {}，房源id：{}", loginPrincipal, houseId);
        UserHouseCollectionAddNewParam userHouseCollectionAddNewParam = new UserHouseCollectionAddNewParam();
        userHouseCollectionAddNewParam.setHouseId(houseId).setUserId(loginPrincipal.getId());
        userHouseCollectionService.addNew(userHouseCollectionAddNewParam);

        return JsonResult.ok();
    }

    @PostMapping("add-new-news-collection")
    @ApiOperation("添加用户收藏资讯")
    @ApiOperationSupport(order = 120)
    public JsonResult addNewNewsCollection(@AuthenticationPrincipal @ApiIgnore LoginPrincipal loginPrincipal,Long newsId){
        log.debug("开始处理添加用户收藏业务,参数:当事人： {}，新闻id：{}", loginPrincipal, newsId);
        UserNewsCollectionAddNewParam userNewsCollectionAddNewParam = new UserNewsCollectionAddNewParam();
        userNewsCollectionAddNewParam.setNewsDetailId(newsId).setUserId(loginPrincipal.getId());
        userNewsCollectionService.addNew(userNewsCollectionAddNewParam);

        return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("删除用户")
    @ApiOperationSupport(order = 200)
    public JsonResult deleteById(@Range(min = 1,message = "根据ID删除用户失败，请提交合法的ID值！")
                                 @RequestParam Long id) {
        log.debug("开始处理【删除用户】的请求，参数：{}", id);
        userService.deleteById(id);
        log.debug("处理【删除用户】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("delete-house-collection")
    @ApiOperation("删除用户收藏房源")
    @ApiOperationSupport(order = 200)
    public JsonResult deleteCollectionByHouseId(@AuthenticationPrincipal LoginPrincipal loginPrincipal,
                                                @Range(min = 1, message = "根据房源ID删除用户失败，请提交合法的ID值！") @RequestParam Long houseId) {
        log.debug("开始处理【删除用户收藏房源】的请求，用户id：{},房源id: {}", loginPrincipal.getId(), houseId);
        userHouseCollectionService.deleteByUserId(loginPrincipal.getId(), houseId);
        log.debug("处理【删除收藏】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("delete-news-collection")
    @ApiOperation("删除用户收藏资讯")
    @ApiOperationSupport(order = 200)
    public JsonResult deleteCollectionByNewsId(@AuthenticationPrincipal LoginPrincipal loginPrincipal,
                                               @Range(min = 1, message = "根据资讯ID删除用户失败，请提交合法的ID值！") @RequestParam Long newsDetailId) {
        log.debug("开始处理【删除用户收藏资讯】的请求，用户id：{},资讯id: {}", loginPrincipal.getId(), newsDetailId);
        userNewsCollectionService.deleteByUserId(loginPrincipal.getId(), newsDetailId);
        log.debug("处理【删除收藏】的请求，完成！");
        return JsonResult.ok();
    }


    @PostMapping("update")
    @ApiOperation("修改用户详情")
    @ApiOperationSupport(order = 300)
    public JsonResult updateById(@AuthenticationPrincipal LoginPrincipal loginPrincipal,@Valid UserUpdateInfoParam userUpdateInfoParam) {
        log.debug("开始处理【修改用户】的请求，参数：{},{}", loginPrincipal.getId(), userUpdateInfoParam);
        userService.updateById(loginPrincipal.getId(), userUpdateInfoParam);
        log.debug("处理【修改用户】的请求，完成！");
        return JsonResult.ok();
    }

    @GetMapping("/standard")
    @ApiOperation("根据ID查询用户详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = false, dataType = "long")
    })
    public JsonResult getStandardById(@AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("开始处理【根据ID查询用户】的请求，参数：{}", loginPrincipal.getId());
        UserStandardVO result = null;
        result = userService.getStandardById(loginPrincipal.getId());
        return JsonResult.ok(result);
    }

    @GetMapping("/standardById")
    @ApiOperation("根据ID查询用户详情")
    @ApiOperationSupport(order = 411)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = false, dataType = "long")
    })
    public JsonResult getStandardById2(@Range(min = 1,message = "请传入合法id值")@RequestParam Long id) {
        log.debug("开始处理【根据ID查询用户】的请求，参数：{}", id);
        UserStandardVO result = userService.getStandardById(id);
        return JsonResult.ok(result);
    }


    @GetMapping("/list")
    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult list(@Range(min = 1, message = "查询用户列表失败，请提供正确的页码值！") Integer page) {
        log.debug("开始处理【查询用户列表】的请求，页码：{}", page);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<UserListItemVO> pageData = userService.list(page);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/houseListByUserId")
    @ApiOperation("根据用户id查询房源")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult listByUserId(@AuthenticationPrincipal LoginPrincipal loginPrincipal,
                                   @Range(min = 1, message = "查询用户列表失败，请提供正确的页码值！") Integer page) {

        log.debug("开始处理【根据用户id查询房源列表】的请求,用户id: {}, 页码:{}", loginPrincipal.getId(), page);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<HouseListItemVO> pageData = userHouseCollectionService.listHouseByUserId(loginPrincipal.getId(), page);
        return JsonResult.ok(pageData);
    }

    @GetMapping("/houseCollectionInfo")
    @ApiOperation("判断用户是否收藏此房源")
    @ApiOperationSupport(order = 450)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "houseId", value = "房源id", paramType = "query")
    })
    public JsonResult houseCollectionInfo(@AuthenticationPrincipal LoginPrincipal loginPrincipal,
                                   Long houseId) {

        log.debug("开始处理【判断用户是否收藏此房源】的请求,用户id: {}, 页码:{}", loginPrincipal.getId(), houseId);
        boolean info = userHouseCollectionService.houseCollectionInfo(loginPrincipal.getId(), houseId);
        return JsonResult.ok(info);
    }

    @GetMapping("/newsCollectionInfo")
    @ApiOperation("判断用户是否收藏此资讯")
    @ApiOperationSupport(order = 480)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "houseId", value = "资讯id", paramType = "query")
    })
    public JsonResult newsCollectionInfo(@AuthenticationPrincipal LoginPrincipal loginPrincipal,
                                          Long newsId) {

        log.debug("开始处理【判断用户是否收藏此资讯】的请求,用户id: {}, 页码:{}", loginPrincipal.getId(), newsId);
        boolean info = userNewsCollectionService.newsCollectionInfo(loginPrincipal.getId(), newsId);
        return JsonResult.ok(info);
    }
}
